﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Windows.ApplicationModel.Resources.Core;
using Windows.Globalization;
using Windows.System.UserProfile;

namespace MishraReader.Entities.Metro.Tests
{
    [TestClass]
    public class StringsTests
    {
        private static readonly object _langLock = new object();

        [TestMethod]
        public void Strings_By_en_Returns_By()
        {
            string actual;
            lock (_langLock)
            {
                ResourceManager.Current.DefaultContext.QualifierValues["Language"] = "en-US";

                actual = Strings.By;
                
                ResourceManager.Current.DefaultContext.Reset();
            }


            Assert.AreEqual("by ", actual);
        }

        [TestMethod]
        public void Strings_By_fr_Returns_par()
        {
            string actual;
            lock (_langLock)
            {
                ResourceManager.Current.DefaultContext.QualifierValues["Language"] = "fr";

                actual = Strings.By;

                ResourceManager.Current.DefaultContext.Reset();
            }

            Assert.AreEqual("par ", actual);
        }

        [TestMethod]
        public void MakeWeak()
        {
            var ec = new EventClass();
            _handlerRaised = false;

            EventHandler<EventArgs> handler = Handler;

            ec.TestEvent += handler.MakeWeak(u => ec.TestEvent -= u);

            ec.RaiseEvent();

            Assert.AreEqual(true, _handlerRaised);

        }

        private void Handler(object sender, EventArgs e)
        {
            _handlerRaised = true;
        }

        private bool _handlerRaised;

        private class EventClass
        {
            public event EventHandler<EventArgs> TestEvent;

            public void RaiseEvent()
            {
                var evt = TestEvent;
                if (evt != null)
                    evt(this, EventArgs.Empty);
            }
        }
    }
}
